package cn.billy.system.controller;

import cn.billy.basic.utils.AjaxResult;
import cn.billy.basic.utils.PageList;
import cn.billy.system.domain.SystemDictionaryDetail;
import cn.billy.system.domain.SystemDictionaryType;
import cn.billy.system.query.SystemQuery;
import cn.billy.system.service.ISystemDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/systemd")
public class SystemDetailController {

    @Autowired
    private ISystemDetailService iSystemDetailService;

    @GetMapping
    public List<SystemDictionaryDetail> loadAll(){
        return iSystemDetailService.loadAll();
    }

    @GetMapping("/{id}")
    public SystemDictionaryDetail getById(@PathVariable("id") Long id){
        return iSystemDetailService.getById(id);
    }


    @PostMapping
    public PageList<SystemDictionaryDetail> queryData(@RequestBody SystemQuery systemQuery){
        PageList<SystemDictionaryDetail> data = iSystemDetailService.queryData(systemQuery);
        return data;
    }

    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            iSystemDetailService.remove(id);
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }
    @DeleteMapping("/del/{id}")
    public AjaxResult remove2(@PathVariable("id") Long id){
        try {
            iSystemDetailService.remove2(id);
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }

    @PutMapping
    public AjaxResult addOrUpt(@RequestBody SystemDictionaryDetail systemDictionaryDetail){
        try {
            if (systemDictionaryDetail.getId() == null){
                iSystemDetailService.save(systemDictionaryDetail);

            } else {
                iSystemDetailService.update(systemDictionaryDetail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }

    }
    @PatchMapping
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            iSystemDetailService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }
}
